package com.dabaozhan.modules.wx_app.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.ValidationBeanUtils;
import com.dabaozhan.modules.wx_app.base.PageResult;
import com.dabaozhan.modules.wx_app.base.ResponseDto;
import com.dabaozhan.modules.wx_app.entity.SupplierSweepEntity;
import com.dabaozhan.modules.wx_app.entity.SupplierUserDeleteEntity;
import com.dabaozhan.modules.wx_app.entity.SupplierUserEntity;
import com.dabaozhan.modules.wx_app.entity.SupplierUserQueryEntitiy;
import com.dabaozhan.modules.wx_app.entity.UserUnionEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.SupplierUserFacade;
import com.dabaozhan.modules.wx_app.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * 供应商模块
 */
@Api(value = "供应商用户模块")
@RestController
@RequestMapping("/wx_app")
public class SupplierUserController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(SupplierUserController.class);
	
	@Autowired
	private SupplierUserFacade supplierUserFacade;
	@Autowired
	private UserService userService;
	
	@ApiOperation(value = "根据姓名或者手机号查询供应商用户")
    @RequestMapping(value = "/getSupplierByQueryInfo")
	public String getSupplierUser(SupplierUserQueryEntitiy entity){
		ResponseDto<List<SupplierUserEntity>> responseDto = new ResponseDto<List<SupplierUserEntity>>();
		try{
			PageResult<List<SupplierUserEntity>> pageResult = supplierUserFacade.getSupplierUserForPage(entity);
			List<SupplierUserEntity> list = pageResult.getListData();
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			responseDto.setResult(list);
			responseDto.setTotal(pageResult.getTotal());
			responseDto.setPage(entity.getPage());
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("根据姓名或者手机号查询供应商用户发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("根据姓名或者手机号查询供应商用户请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "添加供应商用户")
    @RequestMapping(value = "/addSupplierUser")
	public String addSupplierUser(SupplierUserEntity entity){
		ResponseDto<String> responseDto = new ResponseDto<String>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			supplierUserFacade.addSupplierUser(entity);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("添加供应商用户发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("添加供应商用户请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "删除供应商用户")
    @RequestMapping(value = "/deleteSupplierUser")
	public String deleteSupplierUser(SupplierUserDeleteEntity entity){
		ResponseDto<String> responseDto = new ResponseDto<String>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			supplierUserFacade.deleteSupplierUser(entity.getUserId(),entity.getPackerId());
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("删除供应商用户发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("删除供应商用户请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "更新供应商用户")
    @RequestMapping(value = "/updateSupplierUser")
	public String updateSupplierUser(SupplierUserEntity entity){
		ResponseDto<String> responseDto = new ResponseDto<String>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			supplierUserFacade.updateSupplierUser(entity);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("更新供应商用户发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("更新供应商用户请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "根据手机号查询用户身份在银行卡号等信息")
    @RequestMapping(value = "/getUserBaseByPhone")
	public String getUserBaseByPhone(String phone){
		ResponseDto<UserUnionEntity> responseDto = new ResponseDto<UserUnionEntity>();
		try{
			//验证参数是否为空
			UserUnionEntity response =userService.getUserInfoByPhone(phone);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			responseDto.setResult(response);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("根据手机号查询用户身份在银行卡号等信息发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("根据手机号查询用户身份在银行卡号等信息请求参数:{}-返回结果:{}","phone:"+phone,JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "扫码添加供应商")
    @RequestMapping(value = "/sweepSupplier")
	public String sweepSupplier(SupplierSweepEntity entity){
		ResponseDto<SupplierUserEntity> responseDto = new ResponseDto<SupplierUserEntity>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			SupplierUserEntity result = supplierUserFacade.sweepSupplier(entity);
			responseDto.setResult(result);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("扫码添加供应商发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("扫码添加供应商请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}

}
